package com.genesis.yunnanji.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Binder;
import android.support.annotation.Nullable;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.genesis.yunnanji.R;
import com.genesis.yunnanji.bean.BannerBean;
import com.genesis.yunnanji.bean.DiZhouDeliciousFoodBean;
import com.genesis.yunnanji.bean.DiZhouSpecialtyBean;
import com.genesis.yunnanji.config.GenesisApiConfig;
import com.genesis.yunnanji.view.TextBottomActionSheet;
import com.luck.picture.lib.tools.ScreenUtils;

import java.util.HashMap;
import java.util.List;

/**
 * 创建日期：2018/11/1 0001
 * 作者：Kite
 * 描述：美食
 */
public class DiZhouDeliciousFoodAdapter extends BaseQuickAdapter<DiZhouDeliciousFoodBean.ResultBean.ListBean, BaseViewHolder> {

    private Context context;
    private HashMap<String, Boolean> isExpanedable = new HashMap<>();

    public DiZhouDeliciousFoodAdapter(Context context, @Nullable List<DiZhouDeliciousFoodBean.ResultBean.ListBean> data) {
        super(R.layout.item_di_zhou_delicious_food, data);
        this.context = context;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final DiZhouDeliciousFoodBean.ResultBean.ListBean item) {
        ImageView img = (ImageView) helper.getView(R.id.ivImg);
        ViewGroup.LayoutParams layoutParams = img.getLayoutParams();
        layoutParams.width = ScreenUtils.getScreenWidth(context) - ScreenUtils.dip2px(context, 32);
        layoutParams.height = (int) (layoutParams.width / (690 / 360.0f));
        img.setLayoutParams(layoutParams);
        Glide.with(context).load(GenesisApiConfig.PIC_HOST + item.getCover_img()
                + String.format("?x-oss-process=image/resize,m_fill,w_%d,h_%d", layoutParams.width, layoutParams.height)).into(img);
        helper.setText(R.id.tvTitle, item.getTitle());
        helper.setText(R.id.tvAddress, item.getDescription());
        final boolean isExpand = isExpanedable.containsKey(item.getId()) && (isExpanedable.get(item.getId()) != null ? isExpanedable.get(item.getId()) : false);
        helper.setText(R.id.tvPrice, isExpand ? "收起全部" : "展开全部");
        View imgArrow = helper.getView(R.id.imgArrow);
        imgArrow.setRotation(!isExpand ? 0 : 180);
        helper.getView(R.id.btnShowAll).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!isExpand) {
                    TextBottomActionSheet sheet = new TextBottomActionSheet(context, item.getTitle(), item.getDescription());
                    sheet.setOnDismissListener(new DialogInterface.OnDismissListener() {
                        @Override
                        public void onDismiss(DialogInterface dialog) {
                            isExpanedable.clear();
                            notifyDataSetChanged();
                        }
                    });
                    sheet.show();
                }
                isExpanedable.clear();
                isExpanedable.put(item.getId(), true);
                notifyDataSetChanged();
            }
        });
    }

}
